import { reqAddCar, reqDetailList, reqOneshopData } from "@/api"

const state = {
  detailData: {},
  AddCarData: {},
}
const actions = {
  async getDetailData({ commit }, id) {
    const result = await reqDetailList(id)
    if (result.code === 200) {
      commit("GET_DETAIL_DADA", result.data)
    }
  },
  //获取购物车===>也可以添加或者修改购物车数据
  async getAddCarData({ commit }, skuId, isChecked) {
    const result = await reqAddCar(skuId, isChecked)
    if (result.code === 200) {
      commit("GET_ADD_CAR", result.data)
    }
  },
}
const mutations = {
  GET_DETAIL_DADA(status, detail) {
    status.detailData = detail
  },
  GET_ADD_CAR(status, AddCarData) {
    status.AddCarData = AddCarData
  },
}

const getters = {
  // 在一开始时,页面会报错,原因是vuex没有请求数据之前,其中的数据是undefined,是没有的,解决办法,加一个短路运算
  categoryView(state) {
    return state.detailData.categoryView || {}
  },
  skuInfo(state) {
    return state.detailData.skuInfo || {}
  },
  spuSaleAttrList(state) {
    return state.detailData.spuSaleAttrList || []
  },
}
export default {
  namespaced: true,
  state,
  actions,
  mutations,
  getters,
}
